<?php
/**
 * user相关的model实现
 * @文件名称: UserModel.php
 * @author: jawei
 * @Email: gaozhiwei@etcp.cn
 * @Date: 2017-06-06
 * @Copyright: 2017 悦畅科技有限公司. All rights reserved.
 * 注意：本内容仅限于悦畅科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
namespace app\modules\common\models\common;
use app\base\BaseModel;
use app\components\DmpUtil;
use Yii;
use app\components\DmpLog;

class AnnouncementModel extends BaseModel
{
    const STATUS_PUBLIC = 1;//发布状态
    const STATUS_DISABLE = 0;//停用状态

    const IS_SHOW = 1;//是否显示
    const NOT_SHOW = 2;//不显示
    public static function tableName()
    {
        return '{{%announcement}}';
    }
    public static $s_fields = [
        'announcement_id',
        'order_id',
        'role_id',
        'title',
        'content',
        'create_time',
        'update_time',
        'color',
        'department',
        'status',
        'isshow',
    ];
    /**
     * 获取公告数据集合
     * @param array $where
     * @param int $offet
     * @param int $limit
     * @param array $orderBy
     * @param array $field
     * @return $this
     */
    public static function getList($where=[], $offet=0,
        $limit=10, $orderBy=['announcement_id'=>SORT_DESC],$field=['*']) {
        try {
            $query = static::find();
            if(!empty($where) && is_array($where)) {
                $query->where($where);
            }
            if(!empty($orderBy) && is_array($orderBy)) {
                $query->orderBy($orderBy);
            }
            $query->offset($offet);
            $query->limit($limit);
            if(!empty($field) && is_array($field)) {
                $query->select($field);
            }
            return $query->asArray()->all();
//            return $query->createCommand()->getRawSql();
        } catch (\Exception $e) {
            $log = [
                'msg'    => $e->getMessage(),
                'data' => [
                    'where' => $where,
                    'offet' => $offet,
                    'limit' => $limit,
                    'orderBy' => $orderBy,
                    'field' => $field,
                ],
            ];
            DmpLog::log('get_announcement_data_exception', 'error', $log);
            return [];
        }
    }

    /**
     * 根据条件获取公告数据条数
     * @param array $where
     * @return $this
     */
    public static function getTotalLine($where=[]) {
        try {
            $query = static::find();
            if(!empty($where) && is_array($where)) {
                $query->where($where);
            }
            return $query->count();
            return $query->createCommand()->getRawSql();
        } catch (\Exception $e) {
            $log = [
                'msg'    => $e->getMessage(),
                'data' => [
                    'where' => $where,
                    'field' => $field,
                ],
            ];
            DmpLog::log('get_announcement_total_exception', 'error', $log);
            return [];
        }
    }
    /**
     * 通过公告ID获取公告数据
     * @param $announcement_id
     * @param array $field
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function findInfoById($announcement_id,$field=['*']) {
        if((int)$announcement_id) {
            $query = static::find();
            $query->where(['announcement_id'=>$announcement_id]);
            if(!empty($field) && is_array($field)) {
                $query->select($field);
            }
            return $query->asArray()->one();
        }
        return [];
    }
}
